<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Nginx 中文文档 - Server names | Docs4dev</title>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="Nginx (engine x) 是一个高性能的HTTP和反向代理服务，也是一个IMAP/POP3/SMTP服务。">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="true">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta property="og:type" content="website">
<meta property="og:title" content="Nginx 中文文档 - Server names">
<meta property="og:url" content="https://www.docs4dev.com/docs/zh/nginx/current/reference/http-server_names.html">
<meta property="og:site_name" content="Docs4dev">
<meta property="og:locale" content="zh_CN">
<meta name="twitter:card" content="Nginx (engine x) 是一个高性能的HTTP和反向代理服务，也是一个IMAP/POP3/SMTP服务。">
<meta name="generator" content="Docs4dev template engine">
<link rel="stylesheet" href="static/css/app.min.css">
<link rel="shortcut icon" href="https://www.docs4dev.com/static/images/favicon.ico" type="image/x-icon">
<script async="" src="static/js/js.js"></script>
<script async="" src="static/js/adsbygoogle.js" crossorigin="anonymous"></script>
<script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
      dataLayer.push(arguments);
    }

    gtag('js', new Date());
    gtag('config', 'UA-129571937-1');
  </script>
<link rel="amphtml" href="https://www.docs4dev.com/amp/docs/zh/nginx/current/reference/http-server_names.html">
<script type="application/ld+json">{"name":null,"headline":"Nginx 中文文档-Server names","inLanguage":"zh-CN","version":"current","image":"/static/icon/icon-nginx.svg","datePublished":"2021-05-20T12:51:01Z","dateCreated":"2021-05-20T12:51:01Z","dateModified":"2021-07-07T12:48:29Z","@context":"https://schema.org/","@type":"APIReference","abstract":"Nginx (engine x) 是一个高性能的HTTP和反向代理服务，也是一个IMAP/POP3/SMTP服务。"}</script>
</head>
<body>
<div class="book with-summary">
<div class="book-summary">
<div class="logo">
<a href="index.html" style="color: inherit;">
<img src="static/picture/icon-nginx.svg" style="width: 48px; height: 48px;" alt="Logo">
</a>
<b style="color: inherit; margin-left: 8px;">Nginx 中文文档</b>
</div>
<div class="item">
<div>
<label for="version">版本</label>
<select id="version" onchange="onVersionChange(this)">
<option value="current" selected="selected">current</option>
</select>
</div>
<div style="margin-top: 8px;">
<label for="language">语言</label>
<select id="language" onchange="onLangChange(this)" value="zh">
<option value="en">English</option>
<option value="zh" selected="selected">中文</option>
</select>
</div>

</div>
<div class="item menus">
<a title="Table of Contents" style="margin-right: 8px;" href="index.html">
<i class="fa fa-chevron-left"></i>
<span style="margin-left: 2px;">返回目录</span>
</a>
</div>
<nav role="navigation" id="navigation">
<ul class="summary">
<li class="chapter"> <a href="install.html" title="Introduction"> Introduction </a>
<ul class="articles">
<li class="chapter"> <a href="install.html" title="Installing nginx"> Installing nginx </a> </li>
<li class="chapter"> <a href="configure.html" title="从源代码构建  Nginx"> 从源代码构建 Nginx </a> </li>
<li class="chapter"> <a href="beginners_guide.html" title="Beginner's Guide"> Beginner's Guide </a> </li>
<li class="chapter"> <a href="control.html" title="Controlling nginx"> Controlling nginx </a> </li>
<li class="chapter"> <a href="events.html" title="连接处理方法"> 连接处理方法 </a> </li>
<li class="chapter"> <a href="hash.html" title="设置哈希"> 设置哈希 </a> </li>
<li class="chapter"> <a href="debugging_log.html" title="调试日志"> 调试日志 </a> </li>
<li class="chapter"> <a href="syslog.html" title="记录到系统日志"> 记录到系统日志 </a> </li>
<li class="chapter"> <a href="syntax.html" title="配置文件测量单位"> 配置文件测量单位 </a> </li>
<li class="chapter"> <a href="switches.html" title="Command-line parameters"> Command-line parameters </a> </li>
<li class="chapter"> <a href="windows.html" title="Windows 版  Nginx"> Windows 版 Nginx </a> </li>
<li class="chapter"> <a href="http-request_processing.html" title="Nginx 如何处理请求"> Nginx 如何处理请求 </a> </li>
<li class="chapter"> <a href="" title="Server names"> Server names </a> </li>
<li class="chapter"> <a href="http-load_balancing.html" title="使用  Nginx 作为  HTTP 负载均衡器"> 使用 Nginx 作为 HTTP 负载均衡器 </a> </li>
<li class="chapter"> <a href="http-configuring_https_servers.html" title="配置  HTTPS 服务器"> 配置 HTTPS 服务器 </a> </li>
<li class="chapter"> <a href="stream-stream_processing.html" title="Nginx 如何处理  TCP/UDP 会话"> Nginx 如何处理 TCP/UDP 会话 </a> </li>
<li class="chapter"> <a href="njs-index.html" title="用  njs 编写脚本"> 用 njs 编写脚本 </a> </li>
</ul> </li>
<li class="chapter"> <a href="howto_build_on_win32.html" title="How-To"> How-To </a>
<ul class="articles">
<li class="chapter"> <a href="howto_build_on_win32.html" title="使用  Visual C 在  Win32 平台上构建  Nginx"> 使用 Visual C 在 Win32 平台上构建 Nginx </a> </li>
<li class="chapter"> <a href="nginx_dtrace_pid_provider.html" title="使用  DTrace pid 提供程序调试  nginx"> 使用 DTrace pid 提供程序调试 nginx </a> </li>
<li class="chapter"> <a href="http-converting_rewrite_rules.html" title="转换重写规则"> 转换重写规则 </a> </li>
<li class="chapter"> <a href="http-websocket.html" title="WebSocket proxying"> WebSocket proxying </a> </li>
</ul> </li>
<li class="chapter"> <a href="contributing_changes.html" title="Development"> Development </a>
<ul class="articles">
<li class="chapter"> <a href="contributing_changes.html" title="Contributing Changes"> Contributing Changes </a> </li>
<li class="chapter"> <a href="dev-development_guide.html" title="Development guide"> Development guide </a> </li>
</ul> </li>
<li class="chapter"> <a href="dirindex.html" title="Modules reference"> Modules reference </a>
<ul class="articles">
<li class="chapter"> <a href="dirindex.html" title="指令的字母索引"> 指令的字母索引 </a> </li>
<li class="chapter"> <a href="varindex.html" title="变量按字母顺序索引"> 变量按字母顺序索引 </a> </li>
<li class="chapter"> <a href="ngx_core_module.html" title="Core functionality"> Core functionality </a> </li>
<li class="chapter"> <a href="http-ngx_http_core_module.html" title="ngx_http_core_module"> ngx_http_core_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_access_module.html" title="ngx_http_access_module"> ngx_http_access_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_addition_module.html" title="ngx_http_addition_module"> ngx_http_addition_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_api_module.html" title="ngx_http_api_module"> ngx_http_api_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_auth_basic_module.html" title="ngx_http_auth_basic_module"> ngx_http_auth_basic_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_auth_jwt_module.html" title="ngx_http_auth_jwt_module"> ngx_http_auth_jwt_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_auth_request_module.html" title="ngx_http_auth_request_module"> ngx_http_auth_request_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_autoindex_module.html" title="ngx_http_autoindex_module"> ngx_http_autoindex_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_browser_module.html" title="ngx_http_browser_module"> ngx_http_browser_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_charset_module.html" title="ngx_http_charset_module"> ngx_http_charset_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_dav_module.html" title="ngx_http_dav_module"> ngx_http_dav_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_empty_gif_module.html" title="ngx_http_empty_gif_module"> ngx_http_empty_gif_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_f4f_module.html" title="ngx_http_f4f_module"> ngx_http_f4f_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_fastcgi_module.html" title="ngx_http_fastcgi_module"> ngx_http_fastcgi_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_flv_module.html" title="ngx_http_flv_module"> ngx_http_flv_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_geo_module.html" title="ngx_http_geo_module"> ngx_http_geo_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_geoip_module.html" title="ngx_http_geoip_module"> ngx_http_geoip_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_grpc_module.html" title="ngx_http_grpc_module"> ngx_http_grpc_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_gunzip_module.html" title="ngx_http_gunzip_module"> ngx_http_gunzip_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_gzip_module.html" title="ngx_http_gzip_module"> ngx_http_gzip_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_gzip_static_module.html" title="ngx_http_gzip_static_module"> ngx_http_gzip_static_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_headers_module.html" title="ngx_http_headers_module"> ngx_http_headers_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_hls_module.html" title="ngx_http_hls_module"> ngx_http_hls_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_image_filter_module.html" title="ngx_http_image_filter_module"> ngx_http_image_filter_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_index_module.html" title="ngx_http_index_module"> ngx_http_index_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_js_module.html" title="ngx_http_js_module"> ngx_http_js_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_keyval_module.html" title="ngx_http_keyval_module"> ngx_http_keyval_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_limit_conn_module.html" title="ngx_http_limit_conn_module"> ngx_http_limit_conn_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_limit_req_module.html" title="ngx_http_limit_req_module"> ngx_http_limit_req_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_log_module.html" title="ngx_http_log_module"> ngx_http_log_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_map_module.html" title="ngx_http_map_module"> ngx_http_map_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_memcached_module.html" title="ngx_http_memcached_module"> ngx_http_memcached_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_mirror_module.html" title="ngx_http_mirror_module"> ngx_http_mirror_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_mp4_module.html" title="ngx_http_mp4_module"> ngx_http_mp4_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_perl_module.html" title="ngx_http_perl_module"> ngx_http_perl_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_proxy_module.html" title="ngx_http_proxy_module"> ngx_http_proxy_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_random_index_module.html" title="ngx_http_random_index_module"> ngx_http_random_index_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_realip_module.html" title="ngx_http_realip_module"> ngx_http_realip_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_referer_module.html" title="ngx_http_referer_module"> ngx_http_referer_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_rewrite_module.html" title="ngx_http_rewrite_module"> ngx_http_rewrite_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_scgi_module.html" title="ngx_http_scgi_module"> ngx_http_scgi_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_secure_link_module.html" title="ngx_http_secure_link_module"> ngx_http_secure_link_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_session_log_module.html" title="ngx_http_session_log_module"> ngx_http_session_log_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_slice_module.html" title="ngx_http_slice_module"> ngx_http_slice_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_spdy_module.html" title="ngx_http_spdy_module"> ngx_http_spdy_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_split_clients_module.html" title="ngx_http_split_clients_module"> ngx_http_split_clients_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_ssi_module.html" title="ngx_http_ssi_module"> ngx_http_ssi_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_ssl_module.html" title="ngx_http_ssl_module"> ngx_http_ssl_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_status_module.html" title="ngx_http_status_module"> ngx_http_status_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_stub_status_module.html" title="ngx_http_stub_status_module"> ngx_http_stub_status_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_sub_module.html" title="ngx_http_sub_module"> ngx_http_sub_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_upstream_module.html" title="ngx_http_upstream_module"> ngx_http_upstream_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_upstream_conf_module.html" title="ngx_http_upstream_conf_module"> ngx_http_upstream_conf_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_upstream_hc_module.html" title="ngx_http_upstream_hc_module"> ngx_http_upstream_hc_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_userid_module.html" title="ngx_http_userid_module"> ngx_http_userid_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_uwsgi_module.html" title="ngx_http_uwsgi_module"> ngx_http_uwsgi_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_v2_module.html" title="ngx_http_v2_module"> ngx_http_v2_module </a> </li>
<li class="chapter"> <a href="http-ngx_http_xslt_module.html" title="ngx_http_xslt_module"> ngx_http_xslt_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_core_module.html" title="ngx_mail_core_module"> ngx_mail_core_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_auth_http_module.html" title="ngx_mail_auth_http_module"> ngx_mail_auth_http_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_proxy_module.html" title="ngx_mail_proxy_module"> ngx_mail_proxy_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_ssl_module.html" title="ngx_mail_ssl_module"> ngx_mail_ssl_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_imap_module.html" title="ngx_mail_imap_module"> ngx_mail_imap_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_pop3_module.html" title="ngx_mail_pop3_module"> ngx_mail_pop3_module </a> </li>
<li class="chapter"> <a href="mail-ngx_mail_smtp_module.html" title="ngx_mail_smtp_module"> ngx_mail_smtp_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_core_module.html" title="ngx_stream_core_module"> ngx_stream_core_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_access_module.html" title="ngx_stream_access_module"> ngx_stream_access_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_geo_module.html" title="ngx_stream_geo_module"> ngx_stream_geo_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_geoip_module.html" title="ngx_stream_geoip_module"> ngx_stream_geoip_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_js_module.html" title="ngx_stream_js_module"> ngx_stream_js_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_keyval_module.html" title="ngx_stream_keyval_module"> ngx_stream_keyval_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_limit_conn_module.html" title="ngx_stream_limit_conn_module"> ngx_stream_limit_conn_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_log_module.html" title="ngx_stream_log_module"> ngx_stream_log_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_map_module.html" title="ngx_stream_map_module"> ngx_stream_map_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_proxy_module.html" title="ngx_stream_proxy_module"> ngx_stream_proxy_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_realip_module.html" title="ngx_stream_realip_module"> ngx_stream_realip_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_return_module.html" title="ngx_stream_return_module"> ngx_stream_return_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_split_clients_module.html" title="ngx_stream_split_clients_module"> ngx_stream_split_clients_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_ssl_module.html" title="ngx_stream_ssl_module"> ngx_stream_ssl_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_ssl_preread_module.html" title="ngx_stream_ssl_preread_module"> ngx_stream_ssl_preread_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_upstream_module.html" title="ngx_stream_upstream_module"> ngx_stream_upstream_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_upstream_hc_module.html" title="ngx_stream_upstream_hc_module"> ngx_stream_upstream_hc_module </a> </li>
<li class="chapter"> <a href="stream-ngx_stream_zone_sync_module.html" title="ngx_stream_zone_sync_module"> ngx_stream_zone_sync_module </a> </li>
<li class="chapter"> <a href="ngx_google_perftools_module.html" title="ngx_google_perftools_module"> ngx_google_perftools_module </a> </li>
</ul> </li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<header class="book-header">
<div class="dropdown pull-right js-toolbar-action">
<a class="btn toggle-dropdown" aria-label="Language" href="#">
<i class="fa fa-globe"></i>
</a>
<div class="dropdown-menu dropdown-left">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<button class="button size-1" onclick="changeLang('zh_CN')">中文</button>
</div>
<div class="buttons">
<button class="button size-1" onclick="changeLang('en_US')">English</button>
</div>
</div>
</div>
<a class="btn pull-right js-toolbar-action non-mobile" aria-label="Sign up" href="register.html">
<i class="fa fa-sign-in"></i> <span>注册</span>
</a>
<a class="btn pull-right js-toolbar-action non-mobile" aria-label="Sign in" href="register.html">
<i class="fa fa-sign-in"></i>
<span>登录</span>
</a>
<a class="btn pull-left js-toolbar-action btn-summary" href="#"><i class="fa fa-align-justify"></i></a>
<div class="dropdown pull-left font-settings js-toolbar-action">
<a class="btn toggle-dropdown" aria-label="Font Settings" href="#">
<i class="fa fa-font"></i>
</a>
<div class="dropdown-menu dropdown-right">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<button class="button size-2 font-reduce">A</button>
<button class="button size-2 font-enlarge">A</button>
</div>
<div class="buttons">
<button class="button size-2 family-serif">Serif</button>
<button class="button size-2 family-sans">Sans</button>
</div>
<div class="buttons">
<button class="button size-3 theme-white">White</button>
<button class="button size-3 theme-sepia">Sepia</button>
<button class="button size-3 theme-night">Night</button>
</div>
</div>
</div>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Home" href="zh.html">
<i class="fa fa-home"></i> <span>首页</span>
</a>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Guide" href="javascript:window.open('https://www.javadoc.org/');">
<i class="fa fa-book"></i> <span>API Docs</span>
</a>
<a class="btn pull-left js-toolbar-action non-mobile" aria-label="Tools" href="index1.html">
<i class="fa fa-gears"></i> <span>工具</span>
</a>
<div class="dropdown pull-left js-toolbar-action mobile">
<a class="btn toggle-dropdown" aria-label="Language" href="#">
<i class="fa fa-chevron-down"></i>
</a>
<div class="dropdown-menu dropdown-right">
<div class="dropdown-caret"><span class="caret-outer"></span><span class="caret-inner"></span></div>
<div class="buttons">
<a class="button size-1" aria-label="Home" href="zh.html">
<i class="fa fa-home"></i> <span>首页</span>
</a>
</div>
<div class="buttons">
<a class="button size-1" aria-label="Guide" href="javascript:window.open('https://www.javadoc.org/');">
<i class="fa fa-book"></i> <span>API Docs</span>
</a>
</div>
<div class="buttons">
<a class="button size-1" aria-label="Tools" href="index1.html">
<i class="fa fa-gears"></i> <span>工具</span>
</a>
</div>
</div>
</div>
<div id="autocomplete" class="pull-right"></div>
<span id="toolbar-title"></span>
</header>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal markdown-section">
<div id="content">
<h1>Server names</h1>
<div><ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6108808167664152" data-ad-slot="6964403648"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div><ul>
<li><a href="#wildcard_names">Wildcard names</a></li>
<li><a href="#regex_names">正则表达式名称</a></li>
<li><a href="#miscellaneous_names">Miscellaneous names</a></li>
<li><a href="#idn">Internationalized names</a></li>
<li><a href="#optimization">Optimization</a></li>
<li><a href="#compatibility">Compatibility</a></li>
</ul>
<p>服务器名称是使用<a href="http-ngx_http_core_module.html#server_name">server_name</a>指令定义的，并确定哪个<a href="http-ngx_http_core_module.html#server">server</a>块用于给定请求。另请参见“ <a href="http-request_processing.html">Nginx 如何处理请求</a>”。可以使用确切名称，通配符名称或正则表达式来定义它们：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  example.org  www.example.org;
    ...
}

server {
    listen       80;
    server_name  *.example.org;
    ...
}

server {
    listen       80;
    server_name  mail.*;
    ...
}

server {
    listen       80;
    server_name  ~^(?&lt;user&gt;.+)\.example\.net$;
    ...
}
</code></pre>
<p>按名称搜索虚拟服务器时，如果名称匹配多个指定变体之一，例如通配符名称和正则表达式都匹配时，将按照以下优先顺序选择第一个匹配变体：</p>
<ul>
<li>
<p>exact name</p>
</li>
<li>
<p>以星号开头的最长通配符名称，例如“ <code>*.example.org</code>”</p>
</li>
<li>
<p>以星号结尾的最长通配符名称，例如“ <code>mail.*</code>”</p>
</li>
<li>
<p>第一个匹配的正则表达式(按在配置文件中出现的顺序)</p>
</li>
</ul>
<p><a href="" id="wildcard_names"></a></p>
<h4 id="wildcard-names"><a href="#wildcard-names" id="wildcard-names"></a>Wildcard names</h4>
<p>通配符名称只能在名称的开头或结尾以及点边界上包含星号。名称“ <code>www.*.example.org</code>”和“ <code>w*.example.org</code>”无效。但是，可以使用正则表达式指定这些名称，例如“ <code>~^www\..+\.example\.org$</code>”和“ <code>~^w.*\.example\.org$</code>”。星号可以匹配多个名称部分。名称“ <code>*.example.org</code>”不仅与<code>www.example.org</code>匹配，而且也与<code>www.sub.example.org</code>匹配。</p>
<p>形式为“ <code>.example.org</code>”的特殊通配符名称可用于匹配确切名称“ <code>example.org</code>”和通配符名称“ <code>*.example.org</code>”。</p>
<p><a href="" id="regex_names"></a></p>
<h4 id="正则表达式名称"><a href="#正则表达式名称" id="正则表达式名称"></a>正则表达式名称</h4>
<p>nginx 使用的正则表达式与 Perl 编程语言(PCRE)使用的正则表达式兼容。要使用正则表达式，服务器名称必须以波浪号字符开头：</p>
<pre><code class="hljs language-shell">server_name  ~^www\d+\.example\.net$;
</code></pre>
<p>否则，它将被视为确切名称，或者如果表达式包含星号，将被视为通配符名称(最有可能是无效名称)。不要忘记设置“ <code>^</code>”和“ <code>$</code>”锚。在语法上不是必需的，但在逻辑上是必需的。另请注意，域名点应以反斜杠转义。包含字符“ <code>{</code>”和“ <code>}</code>”的正则表达式应加引号：</p>
<pre><code class="hljs language-shell">server_name  &quot;~^(?&lt;name&gt;\w\d{1,3}+)\.example\.net$&quot;;
</code></pre>
<p>否则，nginx 将无法启动并显示错误消息：</p>
<pre><code class="hljs language-shell">directive &quot;server_name&quot; is not terminated by &quot;;&quot; in ...
</code></pre>
<p>命名的正则表达式捕获可以在以后用作变量：</p>
<pre><code class="hljs language-shell">server {
    server_name   ~^(www\.)?(?&lt;domain&gt;.+)$;

    location / {
        root   /sites/$domain;
    }
}
</code></pre>
<p>PCRE 库使用以下语法支持命名捕获：</p>
<div class="alert callout info"><p class="title"><i class="fa fa-info-circle"></i>Note</p><div><table>
<thead></thead>
<tbody>
<tr><td><code>?&lt;name&gt;</code></td><td>Perl 5.10 兼容语法，自 PCRE-7.0 起受支持</td></tr>
<tr><td><code>?'name'</code></td><td>Perl 5.10 兼容语法，自 PCRE-7.0 起受支持</td></tr>
<tr><td><code>?P&lt;name&gt;</code></td><td>自 PCRE-4.0 起受支持的 Python 兼容语法</td></tr>
</tbody>
</table>
</div></div>
<p>如果 nginx 无法启动并显示错误消息：</p>
<pre><code class="hljs language-shell">pcre_compile() failed: unrecognized character after (?&lt; in ...
</code></pre>
<p>这意味着 PCRE 库较旧，应尝试使用语法“ <code>?P&lt;name&gt;</code>”。捕获也可以以数字形式使用：</p>
<pre><code class="hljs language-shell">server {
    server_name   ~^(www\.)?(.+)$;

    location / {
        root   /sites/$2;
    }
}
</code></pre>
<p>但是，这种用法应限于简单的情况(如上述情况)，因为数字参考很容易被覆盖。</p>
<p><a href="" id="miscellaneous_names"></a></p>
<h4 id="miscellaneous-names"><a href="#miscellaneous-names" id="miscellaneous-names"></a>Miscellaneous names</h4>
<p>有些服务器名称经过特殊处理。</p>
<p>如果要求处理不是默认值的<a href="http-ngx_http_core_module.html#server">server</a>块中没有“主机”Headers 字段的请求，则应指定一个空名称：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  example.org  www.example.org  &quot;&quot;;
    ...
}
</code></pre>
<p>如果在<a href="http-ngx_http_core_module.html#server_name">server</a>块中未定义<a href="http-ngx_http_core_module.html#server">server_name</a>，则 nginx 使用空名称作为服务器名称。</p>
<div class="alert callout info"><p class="title"><i class="fa fa-info-circle"></i>Note</p><div><p>在这种情况下，最高 0.8.48 的 nginx 版本将机器的主机名用作服务器名。</p>
</div></div>
<p>如果服务器名称定义为“ <code>$hostname</code>”(0.9.4)，则使用计算机的主机名。</p>
<p>如果有人使用 IP 地址而不是服务器名称发出请求，则“主机”请求 Headers 字段将包含 IP 地址，并且可以使用 IP 地址作为服务器名称来处理请求：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  example.org
                 www.example.org
                 &quot;&quot;
                 192.168.1.1
                 ;
    ...
}
</code></pre>
<p>在所有服务器示例中，可以看到一个奇怪的名称“ <code>_</code>”：</p>
<pre><code class="hljs language-shell">server {
    listen       80  default_server;
    server_name  _;
    return       444;
}
</code></pre>
<p>这个名称没有什么特别的，它只是众多从未与任何真实名称相交的无效域名之一。同样可以使用其他无效名称，例如“ <code>--</code>”和“ <code><a href="email-protection.html" class="__cf_email__" data-cfemail="d3f293">[email&#160;protected]</a>#</code>”。</p>
<p>nginx 最高版本为 0.6.25 的版本支持特殊名称“ <code>*</code>”，该名称被错误地解释为通用名称。它从不用作通用或通配符服务器名称。而是提供了<a href="http-ngx_http_core_module.html#server_name_in_redirect">server_name_in_redirect</a>指令现在提供的功能。现在不赞成使用特殊名称“ <code>*</code>”，而应使用<a href="http-ngx_http_core_module.html#server_name_in_redirect">server_name_in_redirect</a>指令。请注意，无法使用<a href="http-ngx_http_core_module.html#server_name">server_name</a>指令指定全部名称或默认服务器。这是<a href="http-ngx_http_core_module.html#listen">listen</a>指令的属性，而不是<a href="http-ngx_http_core_module.html#server_name">server_name</a>指令的属性。另请参见“ <a href="http-request_processing.html">Nginx 如何处理请求</a>”。可以定义监听端口*：80 和*：8080 的服务器，并指示其中一个将是端口*：8080 的默认服务器，而另一个将是端口*：80 的默认服务器：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    listen       8080  default_server;
    server_name  example.net;
    ...
}

server {
    listen       80  default_server;
    listen       8080;
    server_name  example.org;
    ...
}
</code></pre>
<p><a href="" id="idn"></a></p>
<h4 id="internationalized-names"><a href="#internationalized-names" id="internationalized-names"></a>Internationalized names</h4>
<p>应在<a href="http-ngx_http_core_module.html#server_name">server_name</a>指令中使用 ASCII(Punycode)表示形式来指定国际化域名(<a href="javascript:window.open('https://en.wikipedia.org/wiki/Internationalized_domain_name');" target="_blank" rel="noopener noreferrer">IDNs</a>)：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  xn--e1afmkfd.xn--80akhbyknj4f;  # пример.испытание
    ...
}
</code></pre>
<p><a href="" id="optimization"></a></p>
<h4 id="optimization"><a href="#optimization" id="optimization"></a>Optimization</h4>
<p>确切名称，以星号开头的通配符名称和以星号结尾的通配符名称存储在绑定到侦听端口的三个哈希表中。哈希表的大小在配置阶段进行了优化，因此可以找到 CPU 缓存未命中最少的名称。设置哈希表的详细信息在单独的<a href="hash.html">document</a>中提供。</p>
<p>首先搜索确切名称哈希表。如果未找到名称，则搜索具有以星号开头的通配符名称的哈希表。如果在此处找不到名称，则搜索带有通配符名称以星号结尾的哈希表。</p>
<p>搜索通配符名称哈希表比搜索精确名称哈希表要慢，因为名称是按域部分搜索的。请注意，特殊的通配符形式“ <code>.example.org</code>”存储在通配符名称哈希表中，而不存储在确切的名称哈希表中。</p>
<p>正则表达式是按顺序测试的，因此是最慢的方法，并且不可缩放。</p>
<p>由于这些原因，最好在可能的地方使用确切的名称。例如，如果服务器最常请求的名称是<code>example.org</code>和<code>www.example.org</code>，则显式定义它们的效率更高：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  example.org  www.example.org  *.example.org;
    ...
}
</code></pre>
<p>而不是使用简化形式：</p>
<pre><code class="hljs language-shell">server {
    listen       80;
    server_name  .example.org;
    ...
}
</code></pre>
<p>如果定义了大量服务器名称，或者定义了异常长的服务器名称，则可能有必要在 http 级别调整<a href="http-ngx_http_core_module.html#server_names_hash_max_size">server_names_hash_max_size</a>和<a href="http-ngx_http_core_module.html#server_names_hash_bucket_size">server_names_hash_bucket_size</a>伪指令。 <a href="http-ngx_http_core_module.html#server_names_hash_bucket_size">server_names_hash_bucket_size</a>指令的默认值可以等于 32 或 64，或其他值，具体取决于 CPU 缓存行大小。如果默认值为 32，并且服务器名称定义为“ <code>too.long.server.name.example.org</code>”，则 nginx 将无法启动并显示错误消息：</p>
<pre><code class="hljs language-shell">could not build the server_names_hash,
you should increase server_names_hash_bucket_size: 32
</code></pre>
<p>在这种情况下，指令值应增加到下一个二的幂：</p>
<pre><code class="hljs language-shell">http {
    server_names_hash_bucket_size  64;
    ...
</code></pre>
<p>如果定义了大量服务器名称，则会出现另一条错误消息：</p>
<pre><code class="hljs language-shell">could not build the server_names_hash,
you should increase either server_names_hash_max_size: 512
or server_names_hash_bucket_size: 32
</code></pre>
<p>在这种情况下，首先尝试将<a href="http-ngx_http_core_module.html#server_names_hash_max_size">server_names_hash_max_size</a>设置为接近服务器名称数量的数字。仅当这没有帮助或 nginx 的开始时间过长而无法接受时，才尝试增加<a href="http-ngx_http_core_module.html#server_names_hash_bucket_size">server_names_hash_bucket_size</a>。</p>
<p>如果服务器是侦听端口的唯一服务器，则 nginx 根本不会测试服务器名称(并且不会为侦听端口构建哈希表)。但是，有一个 exception。如果服务器名称是带有捕获的正则表达式，则 nginx 必须执行该表达式以获取捕获。</p>
<p><a href="" id="compatibility"></a></p>
<h4 id="compatibility"><a href="#compatibility" id="compatibility"></a>Compatibility</h4>
<ul>
<li>
<p>从 0.9.4 开始，支持特殊服务器名称“ <code>$hostname</code>”。</p>
</li>
<li>
<p>自 0.8.48 起，默认的服务器名称值为空名称“”。</p>
</li>
<li>
<p>从 0.8.25 开始，已支持命名正则表达式服务器名称捕获。</p>
</li>
<li>
<p>从 0.7.40 开始，支持正则表达式服务器名称捕获。</p>
</li>
<li>
<p>从 0.7.12 开始，支持空服务器名称“”。</p>
</li>
<li>
<p>自 0.6.25 开始，已支持使用通配符服务器名称或正则表达式作为第一个服务器名称。</p>
</li>
<li>
<p>从 0.6.7 开始，支持正则表达式服务器名称。</p>
</li>
<li>
<p>自 0.6.0 开始支持通配符格式<code>example.*</code>。</p>
</li>
<li>
<p>从 0.3.18 开始支持特殊格式<code>.example.org</code>。</p>
</li>
<li>
<p>从 0.1.13 开始支持通配符格式<code>*.example.org</code>。</p>
</li>
</ul>
<table>
<thead></thead>
<tbody>
<tr><td>由 Igor Sysoev 写</td></tr>
<tr><td><br>Brian Mercer 编辑</td></tr>
</tbody>
</table>
</div>
</div>
</section>
<div class="right-sidebar">
<div class="affix"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6108808167664152" data-ad-slot="3102929424" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script data-cfasync="false" src="static/js/email-decode.min.js"></script><script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
</div>
<div class="ft">
<a href="#toolbar-title" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a>
<footer class="footer">
<div class="footer__container--normal">
<img alt="扫码关注公众号" title="扫码关注公众号" src="static/picture/qr-code.png" width="170" height="170">
<div class="footer__description--normal">
<p class="paragraph footer__author--normal">Docs4dev<sup class="super">&#xAE;</sup>
</p>
<p class="paragraph footer__quote&#45;&#45;normal">
如果你在使用过程中遇到任何问题，可以在 <a href="javascript:window.open('https://github.com/docs4dev/docs4dev-issues');" target="_blank" rel="noopener noreferrer">这里<i class="fa fa-external-link"></i></a> 提issue。
</p>
<div class="footer__main--normal">
<p class="paragraph footer__main__paragraph--normal copyright" style="color: #666 !important;">
<a href="javascript:window.open('https://beian.miit.gov.cn/');">
蜀ICP备14021783号-6
</a>
</p>
<p class="paragraph footer__main__paragraph--normal copyright" style="color: #666 !important;">
Copyright &#xA9; Docs4dev all
right reserved, powered by <a href="index2.html" target="_blank">Docs4dev</a></p>
</div>
</div>
</div>
<div class="box__issues">
</div>
</footer>
</div>
</div>
</div>
</div>
</div>
<script>
  var hasToc = true;
  /*  var downloadable = /!*false*!/ false;
    var editable = /!*false*!/ false;
    var code = /!*"nginx"*!/ false;
    var version = /!*"current"*!/ false;
    var type = /!*"reference"*!/ false;
    var lang = /!*"zh"*!/ 'en';
    //edit link
    require(["gitbook", "jQuery"], function (gitbook, $) {
      gitbook.events.bind('start', function (e, config) {
        // Add edit toolbar to left
        var chapterId = /!*17711*!/ 0;
        if (downloadable) {
          gitbook.toolbar.createButton({
            icon: 'fa fa-download',
            text: '下载',
            onClick: function () {
              window.open('/download?code=' + code + '&version=' + version + '&type=' + type + '&lang=' + lang);
            }
          });
        }
        if (editable) {
          gitbook.toolbar.createButton({
            icon: 'fa fa-edit',
            text: '编辑此页',
            onClick: function () {
              window.open('/docs/edit?chapterId=' + chapterId + '&lang=' + lang);
            }
          });
        }
      });
    });*/
</script>
<script>
    var code = "nginx";
    var lang = "zh";
  </script>

<script src="static/js/autocomplete-js.js"></script>
<script src="static/js/app.min.js"></script>
<script src="static/js/search.min.js"></script>
</body>
</html>
